//
//  UserSession.m
//  GolfFriend
//
//  Created by Vescky on 13-11-27.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "UserSession.h"
#import "JSON.h"
#define USER_SESSION @"USER_SESSION"
#define USER_SMSCODE @"USER_SMSCODE"
#define USER_PUBLIC @"USER_PUBLIC_INFO"
#define WEATHER_INFO @"WEATHER_INFO"
#define DEFAULT_TEAM_INFO @"DEFAULT_TEAM_INFO"
#define CHAT_WINDOW @"CHAT_WINDOW"
#define NEARBY_PROPERTY @"NEARBY_PROPERTY"

@implementation UserSession

static UserSession *uSession;

+ (id)shareUserSession {
    if (!uSession) {
        uSession = [[UserSession alloc] init];
    }
    return uSession;
}

- (bool)isLoginned {
    if ([self getUserPrivateInfo]) {
        return YES;
    }
    return NO;
}

#pragma mark - 个人资料修改 Getter & Setter
- (NSString*)getUserId {
    NSDictionary *pDict = [self getUserPrivateInfo];
    return [pDict objectForKey:@"userId"];
}

- (NSString*)getUserName {
    NSDictionary *pDict = [self getUserPublicInfo];
    return [pDict objectForKey:@"nickName"];
}

- (NSString*)getUserAvatar {
    NSDictionary *pDict = [self getUserPublicInfo];
    return [pDict objectForKey:@"avatar"];
}

- (bool)setNickName:(NSString*)_nickName {
    if (isValueSet(_nickName)) {
        return YES;
        NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
        [uds setObject:_nickName forKey:@"nickName"];
        [uds synchronize];
    }
    return NO;
}

- (bool)setEnName:(NSString*)_enName {
    if (isValueSet(_enName)) {
        return YES;
        NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
        [uds setObject:_enName forKey:@"enName"];
        [uds synchronize];
    }
    return NO;
}

- (bool)setUserAvatar:(NSString*)_avatar {
    if (isValueSet(_avatar)) {
        return YES;
        NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
        [uds setObject:_avatar forKey:@"avatar"];
        [uds synchronize];
    }
    return NO;
}

- (NSString*)getTeamName {
    NSDictionary *pDict = [self getUserPublicInfo];
    return [pDict objectForKey:@"teamName"];
}

- (NSString*)getTeamAvatar {
    NSDictionary *pDict = [self getUserPublicInfo];
    return [pDict objectForKey:@"teamAvatar"];
}

- (bool)setTeamName:(NSString*)_tName {
    if (isValueSet(_tName)) {
        return YES;
        NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
        [uds setObject:_tName forKey:@"teamName"];
        [uds synchronize];
    }
    return NO;
}

- (bool)setTeamAvatar:(NSString*)_tAvatar {
    if (isValueSet(_tAvatar)) {
        return YES;
        NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
        [uds setObject:_tAvatar forKey:@"teamAvatar"];
        [uds synchronize];
    }
    return NO;
}

#pragma - mark 球队信息 getDefault TeamInfo
- (NSDictionary*)getDefaultTeamInfo {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    NSDictionary *tDict = [uds objectForKey:DEFAULT_TEAM_INFO];
    return tDict;
}

- (bool)saveDefaultTeamInfo:(NSDictionary*)tInfo {
    if (!tInfo || [tInfo count] < 1) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds setObject:tInfo forKey:DEFAULT_TEAM_INFO];
    [uds synchronize];
    return YES;
}

#pragma - mark 私人信息 Private Info
- (NSDictionary*)getUserPrivateInfo {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [[uds objectForKey:USER_SESSION] JSONValue];
}

- (bool)saveUserInfo:(NSDictionary*)uInfo {
    if (!uInfo || [uInfo count] < 1) {
        return NO;
    }
    NSString *str = [uInfo JSONFragment];
    
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds setObject:str forKey:USER_SESSION];
    [uds synchronize];
    return YES;
}

#pragma - mark 公开信息 Public Info
- (bool)savePublicInfo:(NSDictionary*)pInfo;
{
    if (!pInfo || [pInfo count] < 1) {
        return NO;
    }
    NSString *str = [pInfo JSONFragment];
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds setObject:str forKey:USER_PUBLIC];
    [uds synchronize];
    return YES;
}

- (NSDictionary*)getUserPublicInfo {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [[uds objectForKey:USER_PUBLIC] JSONValue];
}

#pragma mark - 短信验证 SMS Authcode
- (NSString*)getSmsCode {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [uds objectForKey:USER_SMSCODE];
}

- (bool)saveSmsCode:(NSString*)smsCode {
    if (!smsCode) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds setObject:smsCode forKey:USER_SMSCODE];
    [uds synchronize];
    return YES;
}

#pragma mark - 天气 Weather Info
- (bool)saveWeatherInfo:(NSDictionary*)wInfo {
    if (!wInfo) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds setObject:wInfo forKey:WEATHER_INFO];
    [uds synchronize];
    return YES;
}

- (NSDictionary*)getWeatherInfo {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [uds objectForKey:WEATHER_INFO];
}

#pragma mark - 聊天 Recently chat windows
- (bool)saveChatWindow:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    if ([_uId isEqualToString:[[UserSession shareUserSession] getUserId]]) {
        return NO;
    }
    if ([self hasChatWindow:_uId]) {
        return YES;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    NSString *wString = [uds objectForKey:CHAT_WINDOW];
    if (wString && wString.length > 0) {
        NSMutableArray *arr = [wString JSONValue];
        [arr addObject:_uId];
        wString = [arr JSONFragment];
    }
    else {
        NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:_uId, nil];
        wString = [arr JSONFragment];
    }
    if (wString) {
        [uds setObject:wString forKey:CHAT_WINDOW];
        return YES;
    }
    return NO;
}

- (bool)hasChatWindow:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    NSString *wString = [uds objectForKey:CHAT_WINDOW];
    if (wString && wString.length > 0) {
        NSMutableArray *arr = [wString JSONValue];
//        [arr addObject:_uId];
        for (int i = 0; i < [arr count]; i++) {
            if ([[arr objectAtIndex:i] isEqualToString:_uId]) {
                return YES;
            }
        }
    }
    
    return NO;
}

- (bool)deleteChatWindow:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    NSString *wString = [uds objectForKey:CHAT_WINDOW];
    if (wString && wString.length > 0) {
        NSMutableArray *arr = [wString JSONValue];
        [arr removeObject:_uId];
        wString = [arr JSONFragment];
    }
    if (wString) {
        [uds setObject:wString forKey:CHAT_WINDOW];
        return YES;
    }
    return NO;
}

- (NSMutableArray*)getRecetlyChatWindows {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    NSString *wString = [uds objectForKey:CHAT_WINDOW];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[wString JSONValue]];
    [arr removeObject:[[UserSession shareUserSession] getUserId]];
    return arr;
}

#pragma mark - 附近人
//cacheForNearbyUsers = @"xx"(contentString)
- (bool)saveNearbyList:(NSString*)nString {
    if (!nString || nString.length < 1) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds setObject:nString forKey:@"cacheForNearbyUsers"];
    return YES;
}

- (NSString*)getNearbyUserList {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [uds objectForKey:@"cacheForNearbyUsers"];
}

- (NSString*)getDefaultNearbyProperty {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [uds objectForKey:NEARBY_PROPERTY];
}

- (bool)saveDefaultNearbyProperty:(NSString*)p {
    if (p && p.length > 0) {
        NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
        [uds setObject:p forKey:NEARBY_PROPERTY];
        return YES;
    }
    return NO;
}


//历史消息
- (bool)saveHistoryComments:(NSString*)jsonString {
    if (!jsonString || jsonString.length < 1) {
        return NO;
    }
    NSMutableArray *newComments = [[NSMutableArray alloc]initWithArray:[jsonString JSONValue]];
    if (!newComments || [newComments count] < 1) {
        return NO;
    }
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    //合并新旧评论
    NSArray *commentList = [[uds objectForKey:@"cacheForHistoryComments"] JSONValue];
    if (commentList && [commentList count]) {
        [newComments addObjectsFromArray:commentList];
    }
    
    [uds setObject:[newComments JSONFragment]?[newComments JSONFragment]:@"" forKey:@"cacheForHistoryComments"];
    return YES;
}
- (NSString*)getHistoryComments {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    return [uds objectForKey:@"cacheForHistoryComments"];
}

#pragma - mark clean up
- (void)destroyUserInfo {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds removeObjectForKey:USER_SESSION];
}

- (void)cleanSession {
    NSUserDefaults *uds = [NSUserDefaults standardUserDefaults];
    [uds removeObjectForKey:USER_SESSION];
    [uds removeObjectForKey:USER_SMSCODE];
    [uds removeObjectForKey:USER_PUBLIC];
    [uds removeObjectForKey:CHAT_WINDOW];
    [uds removeObjectForKey:DEFAULT_TEAM_INFO];
    [uds removeObjectForKey:@"cacheForHistoryComments"];
}

@end
